package com.example.internet;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库操作类
 */
public class SqlUse {
    private SQLiteDatabase db;
    private Sql sql;

    /**
     * 构造方法
     * @param context
     */
    public SqlUse(Context context){
        sql = new Sql(context);
        db = sql.getWritableDatabase();
    }

    /**
     * 添加方法
     * @param info
     */
    public void add(Computet info) {
        db.execSQL("insert into computerdb (date,num,state1,state2) values (?,?,?,?)", new Object[] {
               info.getDate(), info.getNum(),info.getState1(),info.getState2() });
        db.close();
    }

    /**
     * 查找所有记录方法
     * @return
     */
    public List<Computet> findAll() {
        List<Computet> info = new ArrayList<Computet>();

        Cursor cursor = db.query("computerdb", null, "num !=?", new String[]{""}, null, null, null);
        while (cursor.moveToNext()){
          
            info.add(new Computet(
                    cursor.getString(cursor.getColumnIndex("date")),
                    cursor.getString(cursor.getColumnIndex("num")),
                    cursor.getString(cursor.getColumnIndex("state1")),
                    cursor.getString(cursor.getColumnIndex("state2")))
            );
        }
        cursor.close();// 关闭游标
        db.close();
        return info;// 返回集合
    }

    /**
     *删除方法
     * @param
     * @return
     */
    public int detele(String num) {

        int i = db.delete("computerdb", "num=?",
                new String[]{num});
        db.close();
        return i;
        }


    /**
     * 更新方法
     * @param info
     */
    public void updata(Computet info){
    ContentValues values = new ContentValues();
    values.put("date",info.getDate());
    values.put("num", info.getNum());
    values.put("state1", info.getState1());
    values.put("state2", info.getState2());

     db.update("computerdb", values, "num=?",
            new String[]{info.getNum()});
        db.close();

}
}
